package com.example.administrator.smartdevice.fragment;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.base.BaseFragment;
import com.example.administrator.smartdevice.model.MemberModel;
import com.example.administrator.smartdevice.utils.AgentSharedPreferences;
import com.example.administrator.smartdevice.utils.DisplayHelper;
import com.example.administrator.smartdevice.utils.ServerConstants;
import com.example.administrator.smartdevice.widget.CustomDialog;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/16.
 */

public class MyFragment extends BaseFragment {
    @BindView(R.id.fragment_my_ll_member_information)
    protected LinearLayout fragment_my_ll_member_information;
    @BindView(R.id.fragment_my_img_avatar)
    protected ImageView fragment_my_img_avatar;
    @BindView(R.id.fragment_my_tv_name)
    protected TextView fragment_my_tv_name;
    @BindView(R.id.fragment_my_tv_telephone)
    protected TextView fragment_my_tv_telephone;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected void initData(View view, Bundle savedInstanceState) {
        setStatusBar();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {

    }


    protected void setStatusBar() {
        //设置状态栏颜色和位移一段距离
        DisplayHelper.setStatusBarColor(mBaseActivity, R.color.common_head);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) fragment_my_ll_member_information.getLayoutParams();
        lp.setMargins(0, DisplayHelper.getStatusBarHeight(mBaseActivity), 0, 0);
    }

    protected void initMemberInfo() {
        MemberModel memberModel = AgentSharedPreferences.getMemberInfo(mBaseActivity);
        if (memberModel.getMemberName() != null && !memberModel.getMemberName().equals("")) {
            fragment_my_tv_name.setText(memberModel.getMemberName());
        } else {
            fragment_my_tv_name.setText("未设置");
        }
        fragment_my_tv_telephone.setText(AgentSharedPreferences.getMemberInfo(mBaseActivity).getMemberTelephone());

        if (memberModel.getMemberPic() != null && !memberModel.getMemberName().equals("")) {
            Glide.with(mBaseActivity).load(ServerConstants.FILE_SERVER_URL + memberModel.getMemberPic()).crossFade().into(fragment_my_img_avatar);
        }
    }



    /**
     * 版本更新
     */
    @OnClick(R.id.fragment_my_ll_version_update)
    protected void onVersionUpdateClick() {
        new CustomDialog.Builder(mBaseActivity)
                .setTitle("智能家居")
//                .setMessage("有新的版本，现在更新?")
                .setMessage("当前版本已经是最新版。")
                .setButton(DialogInterface.BUTTON_POSITIVE, "确定", null)
                .setButton(DialogInterface.BUTTON_NEGATIVE, "取消", null)
                .setCancelable(false)
                .create()
                .show();
    }

    /**
     * 关于与帮助
     */
    @OnClick(R.id.fragment_my_ll_about_and_help)
    protected void onAboutAndHelpClick() {
        new CustomDialog.Builder(mBaseActivity)
                .setTitle("智能家居")
                .setMessage("帮助与反馈:1066492750@qq.com")
                .setButton(DialogInterface.BUTTON_POSITIVE, "确认", null)
                .setCancelable(false)
                .create()
                .show();
    }

    /**
     * 退出登录
     */
    @OnClick(R.id.fragment_my_bt_logout)
    protected void onLogoutClick() {
        showInformationDialog("确定要退出登录吗?", DialogInterface.BUTTON_POSITIVE, DialogInterface.BUTTON_NEGATIVE, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    exit();
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initMemberInfo();
    }
}
